/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import java.io.IOException;
import javax.microedition.lcdui.Image;
import ui.ImageLoader;
import xmlutils.LoopData;
import xmlutils.MapXmlData;
import xmlutils.RadarData;
import xmlutils.SatelliteData;
import xmlutils.SlpData;
import xmlutils.TempData;
import xmlutils.VisibilityData;
import xmlutils.WindData;
import xmlutils.ZoomLevelData;

/**
 *
 * @author ayman
 */
public class ImagesFactory {

    private static ImagesFactory imagesFactory;
    private static MapXmlData mapData;
    private int satelliteZoomLevel = -1;
    private int radarZoomLevel = -1;
    private int tempratureZoomLevel = -1;
    private int seaLevelZoomLevel = -1;
    private int windZoomLevel = -1;
    private int visibilityZoomLevel = -1;
    private Image[] satelliteImages;
    private Image[] radarImages;
    private Image tempratureImage;
    private Image seaLevelImage;
    private Image windImage;
    private Image visibilityImage;

    public static ImagesFactory getInstance(MapXmlData xmlData, int initialZoomLevel) {
        if (imagesFactory == null) {
            imagesFactory = new ImagesFactory(initialZoomLevel);
            ImagesFactory.mapData = xmlData;
        }
        return imagesFactory;
    }

    public ImagesFactory(int initialZoomLevel) {
        try {
            this.satelliteImages = this.loadSatelliteImages(initialZoomLevel, true);
            this.radarImages = this.loadRadarImages(initialZoomLevel, true);
            this.tempratureImage = this.loadTempratureImage(initialZoomLevel, true);
            this.seaLevelImage = this.loadSeaLevelPressureImage(initialZoomLevel, true);
            this.windImage = this.loadWindImage(initialZoomLevel, true);
            this.visibilityImage = this.loadVisibilityImage(initialZoomLevel, true);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public Image[] loadSatelliteImages(int zoomLevel) throws IOException {
        return loadSatelliteImages(zoomLevel, false);
    }

    public Image[] loadSatelliteImages(int zoomLevel, boolean forceLoadFromServer) throws IOException {
        if (forceLoadFromServer || zoomLevel != satelliteZoomLevel || satelliteImages == null) {
            SatelliteData satelliteData = mapData.getSatelliteData();
            ZoomLevelData zoomLevelData = satelliteData.getZoomLevelAt(zoomLevel - 1);

            LoopData loopData;
            for (int i = 0; i < zoomLevelData.getLoopsSize(); i++) {
                loopData = zoomLevelData.getLoopAt(i);
                satelliteImages[i] = ImageLoader.getImageFromUrl(loopData.getUrl());
            }
        }
        return this.satelliteImages;
    }

    public Image[] loadRadarImages(int zoomLevel) throws IOException {
        return loadRadarImages(zoomLevel, false);
    }

    public Image[] loadRadarImages(int zoomLevel, boolean forceLoadFromServer) throws IOException {
        if (forceLoadFromServer || zoomLevel != radarZoomLevel || radarImages == null) {
            RadarData radarData = mapData.getRadarData();
            ZoomLevelData zoomLevelData = radarData.getZoomLevelAt(zoomLevel - 1);

            LoopData loopData;
            for (int i = 0; i < zoomLevelData.getLoopsSize(); i++) {
                loopData = zoomLevelData.getLoopAt(i);
                radarImages[i] = ImageLoader.getImageFromUrl(loopData.getUrl());
            }
        }
        return this.radarImages;
    }

    public Image loadTempratureImage(int zoomLevel) throws IOException {
        return loadTempratureImage(zoomLevel, false);
    }

    public Image loadTempratureImage(int zoomLevel, boolean forceLoadFromServer) throws IOException {
        if (forceLoadFromServer || zoomLevel != tempratureZoomLevel || tempratureImage == null) {
            TempData tempData = mapData.getTempData();
            ZoomLevelData zoomLevelData = tempData.getZoomLevelAt(zoomLevel - 1);
            tempratureImage = ImageLoader.getImageFromUrl(zoomLevelData.getImageURL());
        }
        return this.tempratureImage;
    }

    public Image loadSeaLevelPressureImage(int zoomLevel) throws IOException {
        return loadSeaLevelPressureImage(zoomLevel, false);
    }

    public Image loadSeaLevelPressureImage(int zoomLevel, boolean forceLoadFromServer) throws IOException {
        if (forceLoadFromServer || zoomLevel != seaLevelZoomLevel || seaLevelImage == null) {
            SlpData slpData = mapData.getSlpData();
            ZoomLevelData zoomLevelData = slpData.getZoomLevelAt(zoomLevel - 1);
            seaLevelImage = ImageLoader.getImageFromUrl(zoomLevelData.getImageURL());
        }
        return this.seaLevelImage;
    }

    public Image loadWindImage(int zoomLevel) throws IOException {
        return loadWindImage(zoomLevel, false);
    }

    public Image loadWindImage(int zoomLevel, boolean forceLoadFromServer) throws IOException {
        if (forceLoadFromServer || zoomLevel != windZoomLevel || windImage == null) {
            WindData windData = mapData.getWindData();
            ZoomLevelData zoomLevelData = windData.getZoomLevelAt(zoomLevel - 1);
            windImage = ImageLoader.getImageFromUrl(zoomLevelData.getImageURL());
        }
        return this.windImage;
    }

    public Image loadVisibilityImage(int zoomLevel) throws IOException {
        return loadVisibilityImage(zoomLevel, false);
    }

    public Image loadVisibilityImage(int zoomLevel, boolean forceLoadFromServer) throws IOException {
        if (forceLoadFromServer || zoomLevel != visibilityZoomLevel || visibilityImage == null) {
            VisibilityData visibilityData = mapData.getVisibilityData();
            ZoomLevelData zoomLevelData = visibilityData.getZoomLevelAt(zoomLevel - 1);
            visibilityImage = ImageLoader.getImageFromUrl(zoomLevelData.getImageURL());
        }
        return this.visibilityImage;
    }
}
